﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_PledgeEntry_Default : Acms.AcmsControls.TemplatePage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {


        btnEnter.Click += new EventHandler(btnEnter_Click);
        ddlSchoolChoice.DataBinding += new EventHandler(ddlSchoolChoice_DataBinding);
        if (!IsPostBack)
        {
            pnlEnterDetails.Visible = true;
            pnlAlreadyEntered.Visible = pnlPledgeConfirm.Visible = false;
            //flash.Variables = object.Equals ResolveUrl  ("~/pledgeCountXML.xml");
            flash.Variables.Add("xmlurl", ResolveUrl("~/pledgeCountXml.aspx"));
            DataBind();
        }
    }

    void ddlSchoolChoice_DataBinding(object sender, EventArgs e)
    {
        List<Acms.Bll.User> users = Acms.Bll.User.Search(AcmsContext.SiteId);
        List<User> filtered = (from u in users where !string.IsNullOrEmpty(u.GetCustomProperty("School Name")) select u).ToList();
        Acms.Bll.User.Sort(filtered, "School Name");
        foreach (Acms.Bll.User user in filtered)
        {
            ddlSchoolChoice.Items.Add(new ListItem(user.GetCustomProperty("School Name"), user.Id.ToString()));
        }
    }

    void btnEnter_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
            ClimateChangeChampionsData.Pledge pledge = ClimateChangeChampionsData.Pledge.GetByEmailAddress(context, txtEmail.Text.Trim());
            if (pledge != null)
            {
                pnlAlreadyEntered.Visible = true;
            }
            else
            {
                pledge = new ClimateChangeChampionsData.Pledge();
                context.Pledges.InsertOnSubmit(pledge);
                pledge.EmailAddress = txtEmail.Text.Trim();
                pledge.FirstName = txtFirstName.Text.Trim();
                pledge.LastName = txtSecondName.Text.Trim();
                pledge.UserId = int.Parse(ddlSchoolChoice.SelectedValue);
                if (p1.Checked)
                {
                    pledge.PledgeId = 1;
                }
                else if (p2.Checked)
                {
                    pledge.PledgeId = 2;
                }
                else if (p3.Checked)
                {
                    pledge.PledgeId = 3;
                }
                else if (p4.Checked)
                {
                    pledge.PledgeId = 4;
                }
                else if (p5.Checked)
                {
                    pledge.PledgeId = 5;
                }

                context.SubmitChanges();

                Acms.Bll.Email email = Acms.Bll.Email.CreateInstance(pledge.DisplayName, pledge.EmailAddress, Server.MapPath("~/Emails/Pledge.xml"));

                email.Format("DisplayName", pledge.DisplayName);
                email.Format("School Name", ddlSchoolChoice.SelectedItem.Text);
                email.Format("AuthenticationKey", pledge.AuthenticationKey);
                email.Send();
                pnlAlreadyEntered.Visible = false;
                pnlEnterDetails.Visible = false;
                pnlPledgeConfirm.Visible = true;
            }
        }
    }


}
